<template>
  <a-modal :open="open" width="700px" title="手术预约" centered :ok-button-props="{ disabled: !isCompleted }" @cancel="onCancel" @ok="onOk">
    <a-form :model="formState" class="form-style-flex">
      <a-form-item label="手术日期" v-bind="validateInfos.aptmOprDate" class="w-1/2">
        <a-date-picker v-model:value="formState.aptmOprDate" :disabled-date="disabledDate" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="手术时间" v-bind="validateInfos.aptmOprTime" class="w-1/2">
        <a-time-picker v-model:value="formState.aptmOprTime" value-format="HH:mm" format="HH:mm" />
      </a-form-item>
      <a-form-item label="右眼手术" class="w-full">
        <SelectTable
          v-model:value="formState.aptmOdOprBhName"
          type="手术收费项目"
          custom-input
          empty-close
          :search-params="{ brId: store.currentPatient.brId, bw: '右眼', jklb: '5' }"
          @select="onSelect($event, 'aptmOdOprBh', 1, 2)"
        />
      </a-form-item>
      <a-form-item label="左眼手术" class="w-full">
        <SelectTable
          v-model:value="formState.aptmOsOprBhName"
          type="手术收费项目"
          custom-input
          empty-close
          :search-params="{ brId: store.currentPatient.brId, bw: '左眼', jklb: '5' }"
          @select="onSelect($event, 'aptmOsOprBh', 1, 1)"
        />
      </a-form-item>
      <a-form-item label="手术医生" v-bind="validateInfos.aptmOprEmpIdName" class="w-full">
        <SelectTable v-model:value="formState.aptmOprEmpIdName" type="医生" custom-input empty-close @select="onSelect($event, 'aptmOprEmpId', 2)" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import SelectTable from '@/components/SelectTable'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'
import useStore from '../store'

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const isEye = ref(true)
const store = useStore()
const formState = ref({
  aptmOprDate: '',
  aptmOprTime: '',
  aptmOprEmpId: '',
  aptmOprEmpIdName: '',
  aptmOdOprBhName: '',
  aptmOsOprBh: '',
  aptmOsOprBhName: '',
  aptmOdOprBh: '',
  memo: ''
})
const emit = defineEmits(['update:open', 'success'])
const rulesState = ref({
  aptmOprDate: [{ required: true, message: '请选择手术日期' }],
  aptmOprTime: [{ required: true, message: '请选择手术时间' }],
  aptmOprEmpIdName: [{ required: true, message: '请选择手术医生' }]
})
const { resetFields, isCompleted, validateInfos } = useForm(formState, rulesState)
const disabledDate = (current) => {
  // 禁用今天之前的日期
  return current && current < new Date(new Date().toDateString())
}
const onSelect = (selected, key, state, statusId) => {
  formState.value[key] = state === 1 ? selected.bh || selected.sfxmbh : selected.empId
  formState.value[key + 'Name'] = state === 1 ? selected.mc : selected.empName
  if (statusId === 1) {
    formState.value.osAdvId = selected.id
  } else if (statusId === 2) {
    formState.value.odAdvId = selected.id
  }
}
const onOk = async () => {
  if (!formState.value.aptmOsOprBh && !formState.value.aptmOdOprBh) return message.error('必须选择一种手术')
  const res = await post('/operation/opex/doAppointment', {
    ...formState.value,
    brId: store.currentPatient.brId,
    outpInp: 0,
    key: store.currentPatient.jzbh
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  onCancel()
  emit('success')
}
const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

watch(
  () => props.open,
  () => {
    formState.value = { ...props.rowData }
    if (props.rowData?.brId) {
      formState.value.aptmOsOprBhName = props.rowData.osmc
      formState.value.aptmOdOprBhName = props.rowData.odmc
      formState.value.aptmOprEmpName = props.rowData.brName
      formState.value.aptmOprEmpIdName = props.rowData.ssysName
    } else {
      resetFields()
    }
  },
  { immediate: true }
)
watch(
  () => formState.value.aptmOdOprBhName,
  (val) => {
    if (!val) {
      if (!formState.value.aptmOdOprBhName) {
        formState.value.aptmOdOprBh = ''
      }
    }
  }
)
watch(
  () => formState.value.aptmOsOprBhName,
  (val) => {
    if (!val) {
      if (!formState.value.aptmOsOprBhName) {
        formState.value.aptmOsOprBh = ''
      }
    }
  }
)

onMounted(() => {
  isEye.value = getSystemConfig('OO_OPERATION_REGISTER_CHOOSE_MODE') === '1' // 手术0：处置1：全部手术
})
</script>
<style lang="less" scoped></style>
